package com.lh.config;

import com.lh.bean.Candidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.Set;

@Repository
public class CandidateCacheRepository {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostConstruct
    public void init() {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.afterPropertiesSet();
    }

    // 保存候选人信息到 Redis

    // 保存候选人信息到 Redis
    public void saveCandidate(Candidate candidate) {
        String key = "candidate:" + candidate.getJwCode();

        // 确保所有值都是字符串类型
        redisTemplate.opsForHash().put(key, "id", String.valueOf(candidate.getId()));
        redisTemplate.opsForHash().put(key, "jwCode", candidate.getJwCode());
        redisTemplate.opsForHash().put(key, "name", candidate.getName());
        redisTemplate.opsForHash().put(key, "avatar", candidate.getAvatar());
        redisTemplate.opsForHash().put(key, "votes", String.valueOf(candidate.getVotes()));

        // 更新 ZSET
        redisTemplate.opsForZSet().add("candidate:votes", candidate.getJwCode(), candidate.getVotes());
    }

    // 获取候选人详细信息
    public Candidate getCandidate(String jwCode) {
        String key = "candidate:" + jwCode;
        Candidate candidate = new Candidate();

        Object idObj = redisTemplate.opsForHash().get(key, "id");
        if (idObj != null) {
            candidate.setId(Long.parseLong(idObj.toString()));
        }

        candidate.setJwCode((String) redisTemplate.opsForHash().get(key, "jwCode"));
        candidate.setName((String) redisTemplate.opsForHash().get(key, "name"));
        candidate.setAvatar((String) redisTemplate.opsForHash().get(key, "avatar"));

        Object votesObj = redisTemplate.opsForHash().get(key, "votes");
        if (votesObj != null) {
            candidate.setVotes(Integer.parseInt(votesObj.toString()));
        }
        //如果id，jwCode，name，avatar，votes都为空，则返回null
        if (candidate.getId() == null && candidate.getJwCode() == null && candidate.getName() == null && candidate.getAvatar() == null) {
            return null;
        }

        return candidate;
    }

    // 获取所有候选人的 jwCode 按投票数排序
    public Set<Object> getCandidateJwCodesByVotes() {
        return redisTemplate.opsForZSet().reverseRange("candidate:votes", 0, -1);
    }

    // 删除 Redis 中所有候选人数据
    public void deleteAllCandidatesFromCache() {
        Set<Object> jwCodes = redisTemplate.opsForZSet().range("candidate:votes", 0, -1);
        if (jwCodes != null) {
            for (Object jwCode : jwCodes) {
                redisTemplate.delete("candidate:" + jwCode);
            }
        }
        redisTemplate.delete("candidate:votes");
    }
}
